<?php
// +----------------------------------------------------------------------
// | Description: 工作台及基础
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\Field;
use app\admin\model\User;
use think\facade\Request;

class Index extends ApiCommon
{
    /**
     * 用于判断权限
     * @permission 无限制
     * @allow 登录用户可访问
     * @other 其他根据系统设置
     */
    public function initialize()
    {
        parent::initialize();
        $action = [
            'permission' => [],
            'allow' => ['fields', 'fieldrecord', 'authlist','sort','updatesort',
            'importnum','importinfo','importlist','readnotice'],
        ];
        event('CheckAuth',$action);
        $request = Request::instance();
        $a = strtolower($request->action());
        if (!in_array($a, $action['permission'])) {
            parent::initialize();
        }
    }

    /**
     * 权限数据返回
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @author Mr.Liu Wuchen
     * @create 2021-05-22 16:26
     */
    public function authList()
    {
        $userInfo  = $this->userInfo;
        $userModel = new User();
        $dataList  = $userModel->getMenuAndRule($userInfo['id']);
        return resultArray(['data' => $dataList['authList']]);
    }

    /**
     * 获取字段属性，用于筛选或其他操作
     * @return \think\response\Json
     * @author Mr.Liu Wuchen
     * @create 2021-05-24 11:46
     */
    public function fields()
    {
        $param = $this->param;
        $userInfo = $this->userInfo;
        $param['user_id'] = $userInfo['id'];
        $fieldModel = new Field();
        $field_arr = $fieldModel->getField($param);

        # 转form_type类型，用于场景筛选和创建自定义场景
        foreach ($field_arr AS $key => $value) {
            if ($value['field'] == 'address')      $field_arr[$key]['form_type'] = 'map_address';
            if ($value['field'] == 'deal_status')  $field_arr[$key]['form_type'] = 'deal_status';
            if ($value['field'] == 'check_status') $field_arr[$key]['form_type'] = 'check_status';
            if ($param['types'] == 'crm_visit' && $value['field'] == 'owner_user_id') $field_arr[$key]['name'] = '回访人';
        }

        return resultArray(['data' => $field_arr]);
    }


    /**
     * todo
     * 顶部菜单栏展示
     * @return mixed
     */
    //todo admin_sort 表数据未完善
    public function sort(){
        $param = $this->param;
        $userModel = new User();
        $userInfo = $this->userInfo;
        $param['user_id']= $param['user_id'] ?? $userInfo['id'];
        $dataList = $userModel->sortList($param);
        return resultArray(['data' => $dataList]);
    }


}
